<?php

namespace app\model;

use \core\lib\Model;

class BaseModel extends Model
{
    public function isFind(int $id)
    {
        return $this->has($this->title, ["AND" => ['id' => $id]]);
    }

    public function isHave($where)
    {
        return $this->has($this->title, $where);
    }

    /**
     * 模型查找id数据.
     */
    public function find(int $id, $field = '*')
    {
        return $this->get($this->title, $field, ["AND" => ['id' => $id]]);
    }

    /**
     * 模型查找id数据.
     */
    public function have($where, $field = '*')
    {
        return $this->get($this->title, $field, $where);
    }

    /**
     * 模型添加数据规则.
     * @return 当前操作ID
     */
    public function save(array $data)
    {
        if (!isset($data['created_at'])) {
            $data['created_at'] = date('Y-m-d H:i:s');
        }
        $data['updated_at'] = date('Y-m-d H:i:s');
        $this->insert($this->title, $data);
        return $this->id();
    }

    /**
     * 模型删除数据规则.
     * @return BOOL
     */
    public function del(int $id)
    {
        $this->delete($this->title, ['id' => $id]);
        return $this->doo();
    }

    /**
     * 模型删除数据规则.
     * @return BOOL
     */
    public function delAll(array $where)
    {
        $this->delete($this->title, $where);
        return $this->doo();
    }

    /**
     * 模型修改数据规则.
     * @return BOOL
     */
    public function updateRow(int $id, array $data)
    {
        $data['updated_at'] = date('Y-m-d H:i:s');
        $this->update($this->title, $data, ['id' => $id]);
        return $this->doo();
    }

    /**
     * 模型修改数据规则.
     * @return BOOL
     */
    public function updateAll($where, array $data)
    {
        $data['updated_at'] = date('Y-m-d H:i:s');
        $this->update($this->title, $data, $where);
        return $this->doo();
    }

    /**
     * 无分页列表数据.
     *
     * @return data 返回数据集
     */
    public function listAll(array $where = [], $field = '*')
    {
        return $this->select($this->title, $field, $where);
    }

    /**
     * 无分页列表数据2.
     */
    public function lists_all_query($where = '', $field = '*')
    {
        return $this->query('SELECT ' . $field . ' FROM ' . $this->title . ' WHERE ' . $where, [])->fetchAll(\PDO::FETCH_ASSOC);
    }

    /**
     * 分页列表数据.
     * @param page 页码
     * @return data 返回数据集
     */
    public function lists(int $page = 1, int $number = 10, $where_base = [])
    {
        $startRecord = ($page - 1) * $number;
        $where_other = ["LIMIT" => [$startRecord, $number]];
        !isset($where_base['ORDER']) && $where_other['ORDER'] = ["id" => "DESC"];
        if (empty($where_base)) {
            $where = $where_other;
        } else {
            $where = array_merge($where_base, $where_other);
        }
        $data_ar = $this->select($this->title, 'id', $where);
        $where_ar['id'] = !empty($data_ar) ? $data_ar : 0;
        if (isset($where['ORDER'])) {
            $where_ar['ORDER'] = $where['ORDER'];
        }
        return $this->select($this->title, '*', $where_ar);
    }

    /**
     * 获取某一类条数，不传值返回总条数.
     */
    public function rowsCount($where_base = [])
    {
        $data = $this->count($this->title, 'id', $where_base);
        return $data ? $data : 0;
    }

    /*求和*/
    public function findSum($field, $where = [])
    {
        $data = $this->sum($this->title, $field, $where);
        return $data ? $data : 0;
    }
}
